/**
 * First we will load all of this project's JavaScript dependencies which
 * includes Vue and other libraries. It is a great starting point when
 * building robust, powerful web applications using Vue and Laravel.
 */

require('./bootstrap');
import axios from "axios";
import router from "./plugins/router";
import App from './App.vue'
import store from "./plugins/store";
import vuetify from './plugins/vuetify'
import Toasted from 'vue-toasted';
import '@mdi/font/css/materialdesignicons.css' // 引入本地的Material Design Icons资源
import Photoswipe from 'vue-pswipe'


window.Vue = require('vue').default;
Vue.prototype.appName = '';
Vue.use(Toasted, {
    position: "top-center",
    duration: 2000
});



Vue.use(Photoswipe);

Vue.mixin({
    methods: {
        copy: function (h) {
            if (navigator.clipboard) {
                navigator.clipboard.writeText(h).then(function () {
                }, function (err) {
                    console.error('复制失败', err);
                });
            } else {
                alert("浏览器安全禁止复制功能, 请手动复制: " + h);
            }
        },
    },
})

axios.defaults.withCredentials = true;
axios.defaults.headers.post["Content-Type"] = "application/json";
axios.defaults.headers.post["Accept"] = "application/json";
axios.interceptors.request.use(function (config) {
    const token = store.getters.token;
    if (token != null) {
        config.headers["Authorization"] = "Bearer " + token;
    }
    return config;
}, function (error) {
    return Promise.reject(error);
});



axios.interceptors.response.use(function (response) {
    return response;
}, function (error) {
    // Do something with request error
    if (error.response.status == 401) {
        store.commit("user", null);
        store.commit("token", null);
        router.replace({
            path: '/login'
        }).catch(() => { });
        return Promise.reject(true);
    } else {
        return Promise.reject(error);
    }
});

/**
 * Next, we will create a fresh Vue application instance and attach it to
 * the page. Then, you may begin adding components to this application
 * or customize the JavaScript scaffolding to fit your unique needs.
 */

const app = new Vue({
    vuetify,
    router,
    store,
    render: h => h(App)
}).$mount('#app');
